package legumes.client.ui;

import legumes.client.evt.customer.CustomerStoreSelectionChangedEvent;
import legumes.client.evt.customer.CustomerStoreSelectionChangedEventHandler;
import legumes.client.evt.main.ClientsEvent;
import legumes.client.evt.main.ClientsEventHandler;
import legumes.client.evt.main.MyAccountEvent;
import legumes.client.evt.main.MyAccountEventHandler;
import legumes.client.evt.main.StoreSelectionOverviewEvent;
import legumes.client.evt.main.StoreSelectionOverviewEventHandler;
import legumes.client.evt.manager.EventBusManager;
import legumes.client.evt.store.PlaceDeliverySelectionChangedEvent;
import legumes.client.evt.store.PlaceDeliverySelectionChangedEventHandler;
import legumes.client.evt.store.StoreRemovedEvent;
import legumes.client.evt.store.StoreRemovedEventHandler;
import legumes.client.ui.admin.customers.AdminCustomerPanel;
import legumes.client.ui.admin.stall.AdminStallPanel;
import legumes.client.ui.basket.slate.BasketMainPanel;
import legumes.client.ui.otherboxes.AllContentBoxesPanel;
import legumes.client.ui.welcome.WelcomePanel;
import legumes.shared.domain.Order;
import legumes.shared.domain.SellSession;
import legumes.shared.domain.SellSessionForCustomer;
import legumes.shared.domain.Stall;
import legumes.shared.domain.Store;

import com.google.gwt.core.client.Duration;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.events.TabSelectedEvent;
import com.smartgwt.client.widgets.tab.events.TabSelectedHandler;

public class MainCenterPanel extends HLayout {

	private WelcomePanel welcomePanel;
	private BasketMainPanel basketMainPanel;
	private AdminCustomerPanel clientsPanel;
	private AllContentBoxesPanel otherContentBoxesPanel;
//	private TabSet tabSet;
	
	public MainCenterPanel() {
		super();
		setWidth100();
		setHeight100();
		setAlign(Alignment.CENTER);
		
		bindEvents();
		
//		tabSet = new TabSet();
//		tabSet.setTabBarPosition(Side.TOP);  
//        tabSet.setWidth("700");  
//        tabSet.setHeight100();
        
        welcomePanel = new WelcomePanel();
		otherContentBoxesPanel = new AllContentBoxesPanel();
		setMembers(welcomePanel, otherContentBoxesPanel);
	}
	
//	private void onUserConnected(CustomerDTO cdto_){
//		buildPanels(cdto_);
//	}
	
	private void bindEvents(){

		EventBusManager.getInstance().getEventBus().addHandler(
				StoreSelectionOverviewEvent.TYPE, new StoreSelectionOverviewEventHandler() {
					
					@Override
					public void onStoreSelectionChanged(
							StoreSelectionOverviewEvent event) {
						MainCenterPanel.this.onStoreSelectionChanged();
					}
				}
			);
		
		EventBusManager.getInstance().getEventBus().addHandler(
				MyAccountEvent.TYPE, new MyAccountEventHandler() {
					
					@Override
					public void onMyAccountSelected(MyAccountEvent event) {
						MainCenterPanel.this.onMyAccountSelected();
					}
				}
			);
		
		EventBusManager.getInstance().getEventBus().addHandler(
				ClientsEvent.TYPE, new ClientsEventHandler() {
					
					@Override
					public void onClientsSelected(ClientsEvent event) {
						MainCenterPanel.this.onClientsSelected();
					}
				}
			);
		
		
//		EventBusManager.getInstance().getEventBus().addHandler(
//			UserConnectedEvent.TYPE, new UserConnectedEventHandler() {
//				
//				@Override
//				public void onUserConnected(UserConnectedEvent event) {
//					MainCenterPanel.this.onUserConnected( event.getCustomerDTO() );
//				}
//			}
//		);
//		
//		EventBusManager.getInstance().getEventBus().addHandler(
//			UserDisconnectedEvent.TYPE, new UserDisconnectedEventHandler() {
//				
//				@Override
//				public void onUserDisconnected(UserDisconnectedEvent event) {
//					MainCenterPanel.this.onUserDisconnected();
//				}
//			}
//		);
		
		EventBusManager.getInstance().getEventBus().addHandler(
			StoreRemovedEvent.TYPE, 
			new StoreRemovedEventHandler() {
				
				@Override
				public void onStoreRemoved(StoreRemovedEvent event) {
					MainCenterPanel.this.onStoreRemoved(event.getStore());
//					tabSet.removeTab("tab_store_" + event.getStore().getId());
				}
			}
		);
		
		EventBusManager.getInstance().getEventBus().addHandler(
				PlaceDeliverySelectionChangedEvent.TYPE, 
				new PlaceDeliverySelectionChangedEventHandler(){

					@Override
					public void onStoreSelectionChanged(
							PlaceDeliverySelectionChangedEvent event) {
						MainCenterPanel.this.onPlaceDeliverySelectionChanged(event.getStore());
					}
				});
        
		EventBusManager.getInstance().getEventBus().addHandler(
			CustomerStoreSelectionChangedEvent.TYPE, 
			new CustomerStoreSelectionChangedEventHandler(){

				@Override
				public void onStoreSelectionChanged(
						CustomerStoreSelectionChangedEvent event) {
					onCustomerStoreSelectionChanged(event.getStore());
				}
			}
		);
	}
	
	protected void onStoreRemoved(Store store) {
		SC.warn("Not implemented yet !");
	}

	protected void onClientsSelected() {
		if(clientsPanel==null){
			clientsPanel = new AdminCustomerPanel();
		}
		setMembers(clientsPanel, otherContentBoxesPanel);
	}

	protected void onMyAccountSelected() {
		// TODO Auto-generated method stub
		SC.warn("not implemented yet !");
	}

	protected void onStoreSelectionChanged() {
		if(basketMainPanel==null){
			basketMainPanel = new BasketMainPanel(false);
		}
		setMembers(basketMainPanel, otherContentBoxesPanel);
	}

	private void onCustomerStoreSelectionChanged(Store st_){
//		Tab curTab = tabSet.getTab("tab_store_" + st_.getId());
//		if(curTab  == null){
			Store store = st_;
			String tabTitle = store.getStoreName();
			if(tabTitle.length()>20){
				tabTitle = tabTitle.substring(0, 17) + "...";
			}
//			curTab = new Tab(tabTitle, PNResources.INSTANCE.storeIcon().getURL());
//			curTab.setCanClose(true);
//			curTab.setID("tab_store_" + store.getId());
	        final AdminStallPanel asp = new AdminStallPanel(store);
	        setMembers(asp, otherContentBoxesPanel);
	        
//	        curTab.setPane(asp);
//	        curTab.addTabSelectedHandler(new TabSelectedHandler() {
//				
//				@Override
//				public void onTabSelected(TabSelectedEvent event) {
//					asp.refreshData();
//				}
//			});
//	        tabSet.addTab(curTab);
//		}
//		tabSet.selectTab(curTab);
	}
//	protected void onUserDisconnected() {
//		setMembers(mainCenterGridsPanel, otherContentBoxesPanel);
////		tabSet.updateTab(0, null);
//		for(int i=1; i<tabSet.getNumTabs(); i++){
//			GWT.log("Removing tab n°" + i);
//			tabSet.removeTab(i);
//		}
//	}

	private Tab buildOrderTab(String tabID_){
		Tab tOrderpanel = new Tab("Ma Commande");
		tOrderpanel.setID( tabID_ );
		if(basketMainPanel==null){
			basketMainPanel = new BasketMainPanel(false);
		}
        tOrderpanel.setPane(basketMainPanel);
        tOrderpanel.addTabSelectedHandler(new TabSelectedHandler() {
			
			@Override
			public void onTabSelected(TabSelectedEvent event) {
				basketMainPanel.refreshData();
			}
		});
        return tOrderpanel;
	}
	
	private Tab buildClientsTab(){
        Tab tCustomersPanel = new Tab("Clients");
        final AdminCustomerPanel custPanel = new AdminCustomerPanel();
        tCustomersPanel.setPane(custPanel);
        tCustomersPanel.addTabSelectedHandler(new TabSelectedHandler() {
			
			@Override
			public void onTabSelected(TabSelectedEvent event) {
				custPanel.refreshData();
			}
		});
        return tCustomersPanel;
	}
	
	private void onPlaceDeliverySelectionChanged(Store st_){
		if( basketMainPanel == null){
			basketMainPanel = new BasketMainPanel(false);
			setMembers(basketMainPanel, otherContentBoxesPanel);
		}
		
		if(st_ != null && st_.isOpened() && st_.getSellSessionList().size() > 0){
			//TODO SMA retrieve an open and the last on
			SC.say("Store changed : " + st_.getStoreName());
			SellSession ss = st_.getSellSessionList().get(0);
			this.initData(ss.getOrders().get(0), ss.getStall());
		}
	}
	
//	private void buildPanels(CustomerDTO cdto_){
//		boolean hasAdminProfile = false;
//		if(cdto_!=null){
//			hasAdminProfile = cdto_.getRolesAsEnum().ordinal() == CustomerRole.ADMINISTRATOR.ordinal();
//		}
//
//		List<Tab> tabList = new ArrayList<Tab>();
//		
//		String tabID = "tOrderpanelId";
//		Tab tOrderpanel = tabSet.getTab(tabID);
//		if(tOrderpanel==null){
//			tabList.add( buildOrderTab(tabID) );
//		} else {
//			basketMainPanel.refreshData();
//			tabSet.updateTab(tOrderpanel, basketMainPanel);
//			basketMainPanel.redraw();
////			tOrderpanel.setPane(mainCenterGridsPanel);
//		}
//        
//		if(hasAdminProfile){
//			tabList.add( buildClientsTab() );
//		}
//
//        tabSet.setTabs(tabList.toArray(new Tab[0]));
//        
//        setMembers(tabSet, otherContentBoxesPanel);
//	}
	
	public void initData(SellSessionForCustomer ssfc_){
		long creationDate = (long)Duration.currentTimeMillis();
		if( ssfc_.getOrder() == null){
			Order o = new Order();
			o.setId(1);
			o.setDate(creationDate);
			ssfc_.setOrder(o);
		}
		
		basketMainPanel.initData(ssfc_.getOrder(), ssfc_.getStall());
		otherContentBoxesPanel.initData(ssfc_);
	}
	
	public void initData(Order o_, Stall s_){
		basketMainPanel.initData(o_, s_);
	}
	
}
